<template>
    <view class="Contain"> <!-- Contain Start aspectFill -->
        <view class="uswiper">
            <u-swiper :list="list.data" height="400" @click="PrevImage" imgMode="widthFx"></u-swiper>
        </view>
        <view class="price" v-if="ProductInfo.data!=null">
            <block v-if="ProductInfo.data.productdiscountprice!=null && ProductInfo.data.productdiscountprice!=''">
                <view class="a">
                    <text>¥</text>
                    {{ ProductInfo.data.productdiscountprice }}
                </view>
                <view class="b">
                    <text>¥</text>
                    {{ ProductInfo.data.productprice }}
                </view>
            </block>
            <block v-else>
                <view class="a">
                    <text>¥</text>
                    {{ ProductInfo.data.productprice }}
                </view>
                <view class="b"></view>
            </block>
            <view class="c" @click="addFav(ProductInfo.data.id)">已售{{selled}}件 <u-icon :name="favIcon" color="#ff6600" size="22"></u-icon>收藏</view>
        </view> <!-- price End -->

        <view class="title" v-if="ProductInfo.data!=null">【{{ ProductInfo.data.productname }}】{{ ProductInfo.data.productdesc }}</view>
        <!-- <view class="desc">{{ProductInfo.typeKey}}</view> -->

        <!-- 间隔槽 -->
        <u-gap height="15" bg-color="#F7F9FD" margin-top="20" margin-bottom="20"></u-gap>
        <view class="guige mp">
            <u-row gutter="16">
                <u-col span="2" text-align="center">
                    <view class="gray">规格</view>
                </u-col>
                <u-col span="3" text-align="left">
                    <view style="padding-left:10rpx;" class="stext">1件</view>
                </u-col>
                <u-col span="6" text-align="right">
                    <view><!-- <u-icon name="arrow-right" color="#ccc" size="28"></u-icon> --></view>
                </u-col>
            </u-row>
        </view>
        <view class="service mp">
            <u-row gutter="16">
                <u-col span="2" text-align="center">
                    <view class="gray">服务</view>
                </u-col>
                <u-col span="3" text-align="left">
                    <view class="stext">商家包邮</view>
                </u-col>
                <u-col span="4" text-align="left">
                    <view class="stext">7天无理由退货</view>
                </u-col>
				<u-col span="3" text-align="left">
				    <view class="stext">晚发陪</view>
				</u-col>
            </u-row>
        </view>
		<view class="service mp">
            <u-row gutter="16">
                <u-col span="2" text-align="center">
                    <view class="gray"></view>
                </u-col>
                <u-col span="3" text-align="left">
                    <view class="stext">15天价保</view>
                </u-col>
				<u-col span="4" text-align="left">
				    <view class="stext">破损包退换</view>
				</u-col>
				<u-col span="3" text-align="left">
				    <view class="stext">闪电退款</view>
				</u-col>
            </u-row>
        </view>
        <!-- 评价 -->
 
		<u-gap height="15" bg-color="#F7F9FD" margin-top="20" margin-bottom="20"></u-gap>
		
		<!-- 评价 -->
		<view class="pingjia" @click="goComment">
			<view class="pjTitle">
				<view class="a">商品评价 2000+条</view>
				<view class="b">好评度88%<u-icon name="arrow-right" color="#999" size="12"></u-icon></view>
			</view>
			<view>
				 <u-scroll-list indicatorColor="#fff0f0" indicatorActiveColor="#f56c6c" style="width:2000px;overflow-x:scroll;">
				        <view class="pjbox" v-for="(item, index) in pjlist" :key="index">
				           <!-- <image :src="item.thumb"></image> -->
						   <view class="pjbox_left">
							   <u-row justify="space-between" align="center">
								   <u-col span="3" justify="center" textAlign="center">
									   <view class="userimg">
										   <image :src="item.headimg" style="width:60rpx; height:60rpx; border-radius:100%;"></image>
										</view>
								   </u-col>
								   <u-col span="9" justify="start" textAlign="left">
									   <view class="username">{{item.username}}</view>
									   <view class="star">
										   <u-rate :gutter="0" :size="13" readonly :count="item.count" v-model="item.value"></u-rate>
									   </view>
								   </u-col>
							   </u-row>
							   <view class="desc"><text>{{item.desc}}</text></view>
						   </view>
						   <view class="pjbox_right">
						   		<image mode="aspectFill" style="width:100px; height:100px;border-radius:10rpx;" 
								:src="item.pjimgs[0]" @click="preViewPjimgs(item)">
								</image>
								<view class="tags">{{item.pjimgs.length}}张</view>
						   </view>
				        </view>
				    </u-scroll-list>
			</view>
		</view>

        <u-gap height="15" bg-color="#F7F9FD" margin-top="20" margin-bottom="20"></u-gap>

        <view class="HomeAd">
            <u-row gutter="0">
                <u-col span="1.5" text-align="center">
                    <view>
                        <image :src="$globalConfig.shopLogo"></image>
                    </view>
                </u-col>
                <u-col span="7.5" text-align="left">
                    <view>{{$globalConfig.shopName}}</view>
                </u-col>
                <u-col span="3" text-align="right">
                    <!-- <view class="link">店铺首页</view> -->
                </u-col>
            </u-row>
        </view> <!-- HomeAd End -->

        <u-gap height="15" bg-color="#F7F9FD" margin-top="20" margin-bottom="0"></u-gap>

        <view class="detailContent py-2">
            <view class="tabs">
                <u-tabs :list="Tablist" :scrollable="false" :current="current" @change="change"></u-tabs>
            </view>

            <!-- 详情页 -->
            <view class="detailPage" v-if="isDetailPage===true">
                <view v-for="(item,index) in list.data">
                   <image :src="item" mode="widthFix" @click="preViewImage(index)"></image>
				</view>
                <!-- <view v-html="ProductInfo.description"></view> -->
            </view> <!-- detailpage End -->

            <!-- 参数详情 -->
            <view class="paraPage" v-if="isDetailPage===false">
                <!-- <view class="title">产品参数</view> -->
				<!-- <view class="paras"><u-parse :content="productParas.data"></u-parse></view> -->
                <!-- <view class="gray" style="width:100%; text-align:center;">暂无相关参数</view> -->
                <!-- 参数详情 -->
                <view class="paralist">
					<view style="margin-top:10px;">
						<u-row gutter="16">
							<u-col span="3" text-align="right">
								<view>产品型号 :</view>
							</u-col>
							<u-col span="9" text-align="left">
								<view>荣耀9X系列</view>
							</u-col>
						</u-row>
					</view>
					<view style="margin-top:10px;">
						<u-row gutter="16" style="margin-bottom:10px;">
							<u-col span="3" text-align="right">
								<view>内存 :</view>
							</u-col>
							<u-col span="9" text-align="left">
								<view>4GB+64G内存</view>
							</u-col>
						</u-row>
					</view>
					<view style="margin-top:10px;">
						<u-row gutter="16" style="margin-bottom:10px;" align="top">
							<u-col span="3" text-align="right">
								<view>颜色 :</view>
							</u-col>
							<u-col span="9" text-align="left">
								<view>蓝色/黑色/金黄色/红色/橙色</view>
							</u-col>
						</u-row>
					</view>
					<view style="margin-top:10px;">
						<u-row gutter="16" style="margin-bottom:10px;" align="top">
							<u-col span="3" text-align="right">
								<view>保修期 :</view>
							</u-col>
							<u-col span="9" text-align="left">
								<view>3年</view>
							</u-col>
						</u-row>
					</view>
					<view style="margin-top:10px;">
						<u-row gutter="16" style="margin-bottom:10px;" align="top">
							<u-col span="3" text-align="right">
								<view>分期付款 :</view>
							</u-col>
							<u-col span="9" text-align="left">
								<view>￥300 x 3期 / ￥99 x 6期 / ￥50 x 12期 / ￥20x24期</view>
							</u-col>
						</u-row>
					</view>
                </view> 
            </view> 

        </view> <!-- detailContent End -->


        <!-- 加入购物车 -->
        <view class="addToCart">
            <u-popup :show="showCart" mode="bottom" :closeOnClickOverlay="true" @close="closeCart" :height="shopCartPopuHeight">
                <view class="CartOption">
                    <!-- 头部 -->
                    <view class="Head">
                        <u-row gutter="16">
                            <u-col span="3" text-align="center">
                                <view class="pimg" @click="PrevImage_II">
                                    <image v-if="selected_specification_image!=''" :src="selected_specification_image"
                                           mode="aspectFit"></image>
                                    <image v-else src="https://aigxyy.oss-cn-shenzhen.aliyuncs.com/images/shopimg/none.png" mode="aspectFit"></image>
                                </view>
                            </u-col>
                            <u-col span="9" text-align="left">
								<view class="Pform">
									<view class="shopprice"> <text>¥</text>{{shopPrice}}</view>
									<view class="originPrice">原价: {{originalPrice}}</view>
									<view class="kucun gray">剩余{{ inventory }}件</view>									
								</view> <!-- Pform End -->
                            </u-col>
                        </u-row>
                        <view class="close" @click="showCart=false">
                            <u-icon name="close" color="#999" size="33"></u-icon>
                        </view>
                    </view>
                    <!-- 规格 -->
                    <view class="specifications">
                        <view class="tit">规格</view>
                        <view class="para">
                            <!-- 库存不足，灰色显示 item.stock<=0?'NoData':'' -->
                            <view v-for="(item,index) in specification.data" class="block paralists" @click="selectsp(index)"
                                  :class="[specification_index===index?'selected':'',item.stock<=0?'NoData':'']">
                                <view>{{ item.name }}</view>
                                <view class="needDoctor" v-if="item.needDoctor>0">授</view>
                            </view>
                        </view>
                    </view>
                    <!-- 型号 -->
                    <!-- <view class="specifications">
                        <view class="tit">型号</view>
                        <view class="para">
                            <view class="block" v-for="(item,index) in model" @click="selectmodel(index)" :class="model_index===index?'selected':''">{{item}}</view>
                        </view>
                    </view> -->
                    <view class="buybtn">
                        <u-row gutter="16">
                            <u-col span="6" textAlign="left" justify="flex-start">
                                <view class="black mid">购买数量</view>
                            </u-col>
                            <u-col span="6" textAlign="right" justify="flex-end">
                                <view style="text-align:right; display:flex; justify-content:flex-end;">
                                    <u-number-box v-model="value" disabledInput :min="1" @change="valChange"></u-number-box>
                                </view>
                            </u-col>
                        </u-row>
                    </view> <!-- buybtn End -->

                    <view class="comfirmbtn" @click="comfirmbtn">确定</view>

                </view> <!-- CartOption End -->
            </u-popup>
			
			
        </view> <!-- addToCart End -->
		
		
		
		<BottomShoppingCartBar :isDisabledCart="isDisabledCart" :BuyNum="BuyNum" @OpenCart="ProcessCart"></BottomShoppingCartBar>

    </view> <!-- Contain End -->
</template>

<script setup lang="ts">
import { ref,reactive } from 'vue'
import { GoUrl,Toast } from '@/utils/utils'
import { addFavouriteData,deleteFavouritesData,getProductInfoById } from '@/utils/apiFunction'
import {onShow} from '@dcloudio/uni-app'
import { onLoad } from '@dcloudio/uni-app'
import { userStore } from '@/stores/userStore'	
const userstore = userStore();

let favIcon=ref("star");
let isHaveFaved=ref(false); //是否已修改
let shopDetail=reactive<any>({});
let ProductInfo=reactive<any>({data:
		{
				"id":"1",
				"productdiscountprice":"0.01",
				"productprice":"100",
				"productname":"荣耀 9X",
				"productdesc":"二手手机 升降摄前摄像头自拍手机 麒麟810 6.59英寸全面屏4G全网通 9成新 幻夜黑（颜色随机发） 6GB+128GB",
				"description":"<div><img src='https://aigxyy.oss-cn-shenzhen.aliyuncs.com/images/shopimg/sss1.png' style='width:100%;' /></div><div><img src='https://aigxyy.oss-cn-shenzhen.aliyuncs.com/images/shopimg/sss2.png' style='width:100%;' /></div>" 
		}
});
let isDisabledCart=ref(false);
let shopPrice=ref("0");
let originalPrice=ref("0");
let shopCartPopuHeight=ref("700"); //购物车弹窗默认高度
let list=reactive<any>({data:
	[ //轮播图
	"https://img14.360buyimg.com/n1/s450x450_jfs/t1/120963/40/40895/96615/654fa93fF0582566d/d5352e0c10528d67.jpg",
	"https://img14.360buyimg.com/n0/jfs/t1/194002/15/35955/182255/64caff00Fe2d81172/985ffe3bb366e246.jpg",
	"https://img10.360buyimg.com/n1/jfs/t1/219533/16/28593/117006/6501df47F2c312173/390081e925ea6ff5.jpg"
	]
});
let Tablist=reactive<any>([
	{
		name: '图文详情'
	}, {
		name: '商品参数'
	}
]);
let pjlist=reactive<any>([
		{
			headimg:"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F8ca61ee2-dbe4-41a7-93e1-893930c9a3c4%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1702963507&t=a5ccbe099ea3420acc45302d6e7c9e4d",
			username:"小**美",
			count:5,
			value:5,
			desc:"口味很好，同时也说很好吃...非常滴好吃，好好好吃",
			thumb: "https://cdn.uviewui.com/uview/goods/1.jpg",
			pjimgs:[
				"https://img2.baidu.com/it/u=1126959463,12364488&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500",
				"https://img0.baidu.com/it/u=2847707803,3911281768&fm=253&fmt=auto&app=120&f=JPEG?w=1140&h=760",
				"https://img0.baidu.com/it/u=1938483750,3594273200&fm=253&fmt=auto&app=138&f=JPEG?w=781&h=500"
			] //评论图集
		}, {
			headimg:"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2Ffcde7783-032d-4e0f-a4f7-2da03d10c425%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1702979599&t=ef92d12d6e65c2418ae63c9f57c4ee30",
			username:"小**爱",
			count:5,
			value:1,
			desc:"味道还行...",
			thumb: "https://cdn.uviewui.com/uview/goods/2.jpg",
			pjimgs:[
				"https://img2.baidu.com/it/u=1396893050,683115125&fm=253&fmt=auto&app=138&f=JPEG?w=756&h=500",
				"https://img2.baidu.com/it/u=1761020580,2414300876&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500"
			] //评论图集
		}, {
			headimg:"https://img2.baidu.com/it/u=2229997024,3339657040&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500",
			username:"花**花",
			count:5,
			value:2,
			desc:"是我喜欢的那种水果味...",
			thumb: "https://cdn.uviewui.com/uview/goods/3.jpg",
			pjimgs:[
				"https://img2.baidu.com/it/u=2756103876,3797582414&fm=253&fmt=auto&app=138&f=JPEG?w=499&h=323",
			] //评论图集
		}, {
			headimg:"https://img1.baidu.com/it/u=2425296538,2255658679&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=501",
			username:"晓**晓",
			count:5,
			value:4,
			thumb: "https://cdn.uviewui.com/uview/goods/4.jpg",
			desc:"一般般，没有想象中那么好~",
			pjimgs:[
				"https://img2.baidu.com/it/u=3785952303,2138120373&fm=253&fmt=auto&app=120&f=JPEG?w=889&h=500",
				"https://img1.baidu.com/it/u=2716894778,243127979&fm=253&fmt=auto&app=120&f=JPEG?w=1422&h=800",
				"https://img2.baidu.com/it/u=1569315736,1077193046&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500"
			] //评论图集
		}, {
			headimg:"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2Fb1344a0b-961e-4326-87f8-049869fd5729%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1702979758&t=48b7cddac551238d2eb5d10a164b297e",
			username:"小**杏",
			count:5,
			value:2,
			desc:"香香哒，吃了一口，很丝滑!!!",
			thumb: "https://cdn.uviewui.com/uview/goods/5.jpg",
			pjimgs:[
				"https://img0.baidu.com/it/u=2716450962,922309701&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=334",
			] //评论图集
		}
]);
let classifyId=ref("");
let specification=reactive<any>({data:[]});
let specification_index=ref(0);
let selected_specification_image=ref("https://aigxyy.oss-cn-shenzhen.aliyuncs.com/images/shopimg/sss1.png");
let selected_specification_name=ref("");
let selled=ref(88); //已售
let inventory=ref(10); //库存
let productParas=reactive<any>({data:null}) //HtmlContent内容
let model=reactive<string[]>(["大号", "小号"])
let model_index=ref(-1);
let current=ref(0);
let isDetailPage=ref(true);
let showCart=ref(false);
let value=ref(1);
let BuyNum=ref(0);
let Act=ref("");
let isExecuting=ref(false);

//是否直接购买模式
let directByMode=ref(false); 
//购物车对象列表
let cartList=reactive<any>([]);

const goComment=()=>{
	uni.$tools.GoUrl("/pages/shop_comment/shop_comment");
}

const preViewImage=function(index:Number){
	console.log("预览图片:",list.data[index]);
	uni.previewImage({
		current:list.data[index],
		urls:list.data,
	})
}

const preViewPjimgs=function(item:Object){
	uni.previewImage({
		current:item.pjimgs[0],
		urls:item.pjimgs,
	})
}

onLoad((opt:any)=>{
	if(opt.pid && opt.pid!=''){
		//查找用户之前是否已收藏缓存
		isHaveFaved.value=userstore.isFavouritesExist(parseInt(opt.pid));
		console.log("商品之前是否已收藏",isHaveFaved.value);
		if(isHaveFaved.value){
			favIcon.value="star-fill";
		}else{
			favIcon.value="star";
		}
		
		//console.log("产品id",opt.pid)	
		/*
		getProductInfoById(opt.pid).then(res=>{
			//console.log("获取产品信息..",res)
			if(res.data.code===200){
				ProductInfo.data=res.data.data;
				console.log("当前产品信息==》",ProductInfo)
				selled.value=ProductInfo.data.productselled;
				if(ProductInfo.data.productdetailsimg!=null && ProductInfo.data.productdetailsimg!=""){
					list.data=JSON.parse(ProductInfo.data.productdetailsimg);
				}
				//console.log("轮播图==》",list)
				
				//获取产品规格
				if(ProductInfo.data.specificationDatas!=null && ProductInfo.data.specificationDatas.length>0){
					ProductInfo.data.specificationDatas.forEach(item=>{
						var obj:any={};
						obj.id=item.id;  //规格id--注意两者区别
						obj.picUrl=item.spphoto;
						obj.name=item.spname;
						obj.desc=item.spdesc; //使用说明
						obj.stock=item.spstore;
						obj.needDoctor=0; //是否有折扣 //此字段可暂时忽略，不用赋值
						obj.selled=item.spselled;
						obj.discountPrice=item.spdiscountprice;
						obj.originalPrice=item.sporiginprice;
						//console.log("添加对象",obj);
						specification.data.push(obj);
					})
				}else{
					console.log("没有规格产品");
					//没有规格产品，封装当前详情页产品为第一组产品
						var obj:any={};
						obj.id=ProductInfo.data.id;  //主产品id--注意两者区别
						obj.picUrl=ProductInfo.data.productimg;
						obj.name=ProductInfo.data.productname;
						obj.stock=ProductInfo.data.productstore;
						obj.needDoctor=0; //是否有折扣 //此字段可暂时忽略，不用赋值
						obj.selled=ProductInfo.data.productselled;
						obj.discountPrice=ProductInfo.data.productdiscountprice;
						obj.originalPrice=ProductInfo.data.productprice;
						obj.desc=ProductInfo.data.productdesc; //产品描述
						//console.log("添加对象",obj);
						specification.data.push(obj);					
				}
				console.log("当前产品拥有的规格==>",specification.data)
				selected_specification_image.value = specification.data[0].picUrl;
				shopPrice.value= specification.data[0].discountPrice;
				originalPrice.value= specification.data[0].originalPrice;
				inventory.value= specification.data[0].stock;
								
				//产品参数
				productParas.data=ProductInfo.data.productparas;
				
				//路由记录,登录后返回
				if(opt.noRecord==null){ //登录后返回，不再记录
					userstore.setBackRouter("../shop_detail/shop_detail?pid="+opt.pid);
				}
				
				userstore.add(opt.pid,ProductInfo.data);
			}
		});
		*/
	}
})

onShow(()=>{
	uni.removeStorageSync("BuyProductInfo");
	uni.removeStorageSync("cartProductList");
})

//添加收藏
const addFav=async (productId:number)=>{
	
	if(userstore.userData.isLogin==false){
		Toast("请登录后操作");
		setTimeout(()=>{
			GoUrl("../mine/mine",2);
		},1000);
		return;
	}
	
	//判断之前是否已收藏过
	isHaveFaved.value=userstore.isFavouritesExist(productId);
	if(isHaveFaved.value){
		//已收藏
		let favId:number=userstore.getCurFavId(); // getCurFavId 依赖于 isFavouritesExist
		let res:any=await deleteFavouritesData(favId);
		if(res.data.code===200){
			favIcon.value="star";
			userstore.removeFavourites(favId); //删除缓存
		}
	}else{
		//未收藏
		let userId:number=userstore.userData.userinfo.id || -1;
		if(userId!=-1){
			let res:any=await addFavouriteData(userId,productId);
			console.log("收藏res==>",res);
			if(res.data.code===200){
				favIcon.value="star-fill";
				isHaveFaved.value=true;
				let FavouritesData:favouritesType={
						favId:res.data.favId,
						userId:userId,
						productId:productId
				}
				userstore.addFavourites(FavouritesData);
				Toast("已收藏");
			}
		}		
	}
}


const closeCart=()=>{
	showCart.value=false;
}
//放大预览轮播图
const PrevImage=(e:any)=>{
	console.log(e,list);
	//重新改变list架构适应预览接口
	let Temp =reactive<any>([]);
	list.data.forEach((item:any) => {
		Temp.push(item);
	})
	list.data = Temp;
	uni.previewImage({
		urls:list.data,
		current:list.data[e],
	});
}
//放大预览轮播图
const PrevImage_II=()=>{
	let Temp = reactive<any>([]);
	Temp.push(selected_specification_image.value);
	uni.previewImage({
		urls: Temp,
		current: Temp[0],
	});
}

const selectsp=(index:number)=>{
	console.log("规格索引:",index,"对象:",specification.data[index]);
	specification_index.value = index;
	selected_specification_name.value = specification.data[index].name;
	selected_specification_image.value = specification.data[index].picUrl;
	//选择规格后，库存变化
	inventory.value = specification.data[index].stock;
	classifyId.value = specification.data[index].id;
	//更新追加的字段值
	ProductInfo.data.desc= specification.data[index].desc;
	ProductInfo.data.classifyId = specification.data[index].id;
	ProductInfo.data.classifyName = specification.data[index].name;
	ProductInfo.data.needDoctor = specification.data[index].needDoctor;
	ProductInfo.data.picUrl = specification.data[index].picUrl;
	//显示当前选中的价格
	if(specification.data[index].discountPrice!=null && specification.data[index].discountPrice!=""){
		shopPrice=specification.data[index].discountPrice;
		originalPrice=specification.data[index].originalPrice;
	}else{
		if(specification.data[index].originalPrice!=null && specification.data[index].originalPrice!=""){
			shopPrice.value=specification.data[index].originalPrice;
		}else{
			shopPrice.value="暂无售价"
		}						
	}
	console.log("选择的规格", specification, selected_specification_name);
	
	//保存选中的规格
	//uni.setStorageSync("specificationIndex",index);
}

const selectmodel=(index:number)=>{
     model_index.value = index;
}

const comfirmbtn=()=>{
	
	//当前规格库存不足
	if (inventory.value <= 0) {
		Toast("当前选择的商品规格库存不足");
		return;
	}
	showCart.value = false;

	//获取上次选择的的组件index
	/* if(uni.getStorageSync("specificationIndex")){
		specification_index=uni.getStorageSync("specificationIndex");
		ProductInfo.data.picUrl = specification.data[specification_index.value].picUrl;
	} */
	
	if (Act.value === '购物车') {
		
		directByMode.value=false;
		uni.setStorageSync("directByMode",directByMode.value);
		
		//这里要转换为购物车对应的字段
		/*
			id=BuyProductInfo.data.id;
			isOver=0; //保持默认
			checked=false; //保持默认
			discountPrice=BuyProductInfo.data.productdiscountprice;
			picUrl=BuyProductInfo.data.productimg;
			needDoctor=0; //这个字段默认0，可以忽略
			goodsName=BuyProductInfo.data.productname;
			classifyName=BuyProductInfo.data.productdesc; //商品描述
			num=BuyProductInfo.data.BuyNum;
		*/
	   
		ProductInfo.data.id=specification.data[specification_index.value].id; //这里是购物车模式的话，id=规格的id 非 产品id
		ProductInfo.data.productdiscountprice=specification.data[specification_index.value].discountPrice;   //规格的价格 -- 注意2者区别
		ProductInfo.data.productprice=specification.data[specification_index.value].originalPrice;
		ProductInfo.data.productimg = specification.data[specification_index.value].picUrl; //规格图 --  注意2者区别
		ProductInfo.data.productname=specification.data[specification_index.value].name;
		ProductInfo.data.productdesc=specification.data[specification_index.value].desc;
		ProductInfo.data.BuyNum = value.value;
		
		//删除属性
		delete ProductInfo.data.specificationDatas;
		
		
		console.log("当前选中商品信息id:",ProductInfo.data.id,ProductInfo.data);
		
		if(uni.getStorageSync("ShopCartList")){
			cartList=uni.getStorageSync("ShopCartList");
		}
		
		//购物车列表模式
		let index=cartList.findIndex(item=>{
				console.log("item.id:",item.id,"ProductInfo.data.id:",ProductInfo.data.id);
				return item.id===ProductInfo.data.id
			}
		);
		if(index===-1){
			cartList.push(ProductInfo.data);	
			uni.setStorageSync("ShopCartList",cartList);
		}
		
		//购物车组件响应值
		//购物车是一个列表存储
		if(uni.getStorageSync("ShopCartList")){
			BuyNum.value=cartList.length;
		}else{
			BuyNum.value=1; //初始化，第一次赋值 
		}
		console.log("当前购买数量:",BuyNum.value);
		
		//GoUrl("../shop_order/shop_order");
		return;
	}
}


const change=(e:any)=>{
	current = e.index;
	//console.log(index)
	if (e.index === 1) {
		isDetailPage.value = false;
	} else {
		isDetailPage.value = true;
	}
}


const ProcessCart=(e:any)=>{
	console.log("接收购物车:", e);
	Act.value = e.act;
	//立即购买
	if (Act.value === '立即购买') {
		directByMode.value=true;
		uni.setStorageSync("directByMode",directByMode.value);
		//拼接追加一个购买数量
		ProductInfo.data.BuyNum = value.value;
		//console.log("当前选中商品信息:", ProductInfo.data);
		uni.setStorageSync("BuyProductInfo", ProductInfo.data);
		GoUrl("../shop_car/shop_car",2);
		return;
	}
	showCart.value = e.show; //显示购买规格
	uni.hideLoading();
}


const valChange=(e:any)=>{
	console.log('当前值为: ' + e.value)
	value.value = e.value;
}
</script>






<style>
page {
    padding-bottom: 200 rpx;
}
.u-icon__icon{ margin-left:0px !important; }
.vue-ref{ margin-top:10rpx !important; }
</style>
<style scoped lang="scss">
@import "shop_detail.scss";
:deep(){
	.u-icon__icon{
		margin-left:20rpx;	
	}
}
</style>